﻿using Polenter.Serialization;
using SmartMotion.Core.Mgrs;
using SmartMotion.Core.Tools;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace SmartMotion.Core
{
    public class Constructor : ActionItem
    {
        [ExcludeFromSerialization]
        public ConstructorInfo InnerConstructor { get; internal set; }

        public override UIForm GetConfigForm()
        {
            return new FrmConstructor(this);
        }

        public override bool Initilize()
        {
            try
            {
                string fileName = Environment.CurrentDirectory + "\\" + Dll;
                var curAssembly = Assembly.LoadFile(fileName);
                var type = curAssembly.GetType(TypeName);
                InnerConstructor = type.GetConstructor(ParaTypes.Select(Type.GetType).ToArray());
            }
            catch (Exception e)
            {
                Em.GetEvent<DebugMessage>().Publish($"初始化{ItemName}时出现异常:{e.Message}");
            }
            return true;
        }

        protected override ActionResult RunMethod()
        {
            try
            {
                if (InnerConstructor == null)
                {
                    Initilize();//通过反射拿到构造函数
                }

                //反射执行构造函数创建对象

                #region 参数列表

                var objs = new List<object>();//参数列表对象
                foreach (var item in Parms)
                {
                    object temp;

                    if (item.VarSelected)
                    {
                        var varInfo = VarMgr.Ins.GetVarByName(item.VarName);

                        temp = varInfo.Value;
                    }
                    else
                    {
                        //把手动输入的字符串转换为正确的类型
                        if (item.Type == typeof(int).FullName) { temp = Convert.ToInt32(item.VarName); }
                        else if (item.Type == typeof(uint).FullName) { temp = Convert.ToUInt32(item.VarName); }
                        else if (item.Type == typeof(short).FullName) { temp = Convert.ToInt16(item.VarName); }
                        else if (item.Type == typeof(ushort).FullName) { temp = Convert.ToUInt16(item.VarName); }
                        else if (item.Type == typeof(byte).FullName) { temp = Convert.ToByte(item.VarName); }
                        else if (item.Type == typeof(sbyte).FullName) { temp = Convert.ToSByte(item.VarName); }
                        else if (item.Type == typeof(long).FullName) { temp = Convert.ToInt64(item.VarName); }
                        else if (item.Type == typeof(ulong).FullName) { temp = Convert.ToUInt64(item.VarName); }
                        else if (item.Type == typeof(float).FullName) { temp = Convert.ToSingle(item.VarName); }
                        else if (item.Type == typeof(double).FullName) { temp = Convert.ToDouble(item.VarName); }
                        else if (item.Type == typeof(decimal).FullName) { temp = Convert.ToDecimal(item.VarName); }
                        else if (item.Type == typeof(bool).FullName) { temp = Convert.ToBoolean(item.VarName); }
                        else
                        {
                            temp = item.VarName;
                        }
                    }

                    if (temp != null)
                    {
                        objs.Add(temp);
                    }
                }

                object[] objArray = null;
                if (objs.Count > 0)
                {
                    objArray = objs.ToArray();
                }

                #endregion 参数列表

                #region 执行构造函数

                var result = InnerConstructor.Invoke(objArray);

                #endregion 执行构造函数

                #region 处理构造出来的对象

                if (!ReturnType.Contains("Void") && !string.IsNullOrEmpty(ReturnObjName))
                {
                    var varObj = VarMgr.Ins.GetVarByName(ReturnObjName);
                    varObj.Value = result;
                }

                #endregion 处理构造出来的对象

                return ActionResult.Success;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return ActionResult.Error;
            }
        }
    }
}